<?php
if (!defined('ROOT_PATH')) die('No direct access.');
function createResi($id){
	$db = new db();
	$tot = $db->query("SELECT count(*) as jml FROM Transaksi");
	$tot = (int)$tot[0]['jml']+1;
	$notrans = date("ymd").$id.$tot;	//bikin nomor transaksi
	return $notrans;
}

function cekAlamat($nama,$kota,$kontak,$alamat,$idpembeli){
	$db = new db();
	$query = "SELECT count(*) as jml FROM Alamat WHERE `idPembeli`=$idpembeli AND `kota`='$kota' AND `lengkap`='$alamat' AND `nama_penerima`='$nama' AND `no_kontak`='$kontak'";
	//echo $query;
	$tot = $db->query($query);
	if ($tot[0]['jml']<1){
		//masukkin
		$query = "INSERT INTO Alamat (idPembeli,kota,lengkap,nama_penerima,no_kontak) VALUES ($idpembeli,'$kota','$alamat','$nama','$kontak')";
		$tot = $db->exec($query);
	}
	$id = $db->query("SELECT * FROM Alamat WHERE idPembeli=$idpembeli AND kota='$kota' AND lengkap='$alamat' AND nama_penerima='$nama' AND no_kontak='$kontak'");
	return $id[0]['idAlamat'];
}

function addTransaksi($invoice,$tgl,$total,$diskon,$idAlamat,$idPembeli){
	$db = new db();
	$query = "INSERT INTO Transaksi (`no-invoice`,`tanggal`,`status`,`total-pesanan`,`diskon`,`idAlamat`,`idPembeli`) VALUES ('$invoice','$tgl',1,$total,$diskon,$idAlamat,$idPembeli)";
	$db->exec($query);
	return $db->lastInsertId();
}

function addPesanan($idTrans,$idPenjual,$idBarang,$qty,$metode,$biaya,$isPre){
	$db = new db();
	$stat = $isPre ? 4 : 1;
	$query = "INSERT INTO Pesanan (`idTransaksi`,`idPenjual`,`idBarang`,`kuantitas`,`metode`,`biayakirim`,`status`) VALUES ($idTrans,$idPenjual,$idBarang,$qty,$metode,$biaya,$stat)";
	//echo $query;
	//die();
	$db->exec($query);
}

function konfBayar($invoice,$idfb,$idtrans){
	$db = new db();
	//ubah status transaksi ?perlu idfb
	$orang = getUser($idfb);
	$orang = $orang['nama'];
	$query = "UPDATE  `Transaksi` SET  `status` =  '2' WHERE  `idTransaksi` = $idtrans AND  `no-invoice` =  '$invoice'";
	$db->exec($query);
	//echo $query."<br />";
	//for each pesanan, kirim sms
	$query = "SELECT pes.idTransaksi,bar.nama,pes.kuantitas,pes.biayakirim,pen.kontak,pen.fb_username,pen.idFB,pen.email,pes.idPesanan
			FROM Pesanan pes
			INNER JOIN Barang bar ON pes.idBarang = bar.idBarang
			INNER JOIN Pengguna pen ON pes.idPenjual = pen.idPengguna
			WHERE pes.idTransaksi = $idtrans";
	$pesans = $db->query($query);
	//echo $query."<br />";
	foreach ($pesans as $pesan){
		//var_dump($pesan);
		//update
		$query = "UPDATE  `Pesanan` SET  `status` =  '2' WHERE  `idTransaksi` = $idtrans";
		$db->exec($query);
		//echo $query."<br />";
		//---------SMS
		$barang = $pesan['nama'];
		$qty = $pesan['kuantitas'];
		$biaya = $pesan['biayakirim'];
		$msg = "Ada pesanan($invoice) dari $orang untuk '$barang' sebanyak $qty buah, dengan biaya kirim ".int_to_rp($biaya);
		sendSMS($pesan['kontak'],$msg);
		//---------email
		$uname = $pesan['fb_username'];
		$idfb = $pesan['idFB'];
		$idPesanan = $pesan['idPesanan'];
		$email1 = "$uname@facebook.com";
		$email2 = $pesan['email'];
		$link = TRANSAKSI_URL."kirim/$idPesanan/$idfb/$idtrans/";
		$pesan = "KIRIM \n Barang sudah dibayar pemesan, silakan konfirmasi pengiriman pesanan($invoice) untuk $orang untuk '$barang' sebanyak $qty buah disini $link";
		//echo $pesan."<br />";
		$subject = "Konfirmasi Pengiriman #$invoice";
		$from_name = "Admin Realita";
		sendEmail($email1,$pesan,$subject,$from_name);
		sendEmail($email2,$pesan,$subject,$from_name);
	}
}

function konfKirim($pesanan,$idfb,$idtrans){
	$db = new db();
	//ubah status pesanan
	$query = "UPDATE  `Pesanan` SET  `status` =  '3' WHERE  `idPesanan` = $pesanan";
	echo $query."<br />";
	$db->exec($query);
	//SMS pembeli
	$query = "SELECT kontak FROM `Transaksi` INNER JOIN Pengguna ON Transaksi.idPembeli=Pengguna.idPengguna WHERE idTransaksi = $idtrans";
	echo $query."<br />";
	$kontak = $db->query($query);
	$kontak = $kontak[0]['kontak'];
	$query = "SELECT nama FROM `Pesanan` INNER JOIN Barang ON Pesanan.idBarang = Barang.idBarang WHERE idPesanan = $pesanan";
	echo $query."<br />";
	$barang = $db->query($query);
	$barang = $barang[0]['nama'];
	$trans = getTrans($idtrans);
	$invoice = $trans['no-invoice'];
	$idpembeli = $trans['idPembeli'];
	$msg = "Pesanan $barang pada transaksi #$invoice telah dikirim";
	sendSMS($kontak,$msg);
	//------------EMAIL
	/*$uname = $pesan['fb_username'];
	$idfb = $pesan['idFB'];
	$idPesanan = $pesan['idPesanan'];
	$email1 = "$uname@facebook.com";
	$email2 = $pesan['email'];
	$link = TRANSAKSI_URL."kirim/$idPesanan/$idfb/$idtrans/";
	$pesan = "KIRIM \n Barang $barang pada #$invoice sudah dikirim";
	//echo $pesan."<br />";
	$subject = "Notifikasi Pengiriman #$invoice";
	$from_name = "Admin Realita";
	sendEmail($email1,$pesan,$subject,$from_name);
	sendEmail($email2,$pesan,$subject,$from_name);*/
	//----------------
	$query = "SELECT * FROM Pesanan WHERE idTransaksi = $idtrans AND status < 3";
	echo $query."<br />";
	$pesans = $db->query($query);
	//cek transaksi masih ada pesanan
	if (count($pesans)<1){
		//ubah status transaksi
		$query = "UPDATE  `Transaksi` SET  `status` =  '3' WHERE  `idTransaksi` = $idtrans";
		echo $query."<br />";
		$db->exec($query);
		//tambah poin pembeli
		$query = "UPDATE  `Pengguna` SET  `poin_reward` =  `poin_reward`+10 , `poin_rating-pembeli` = `poin_rating-pembeli`+10  WHERE  `idPengguna` = $idpembeli";
		echo $query."<br />";
		$db->exec($query);
		//tambah poin penjual
		$query = "SELECT idPenjual FROM Pesanan WHERE idTransaksi = $idtrans";
		echo $query."<br />";
		$penjuals = $db->query($query);
		foreach ($penjuals as $penjual){
			$idpenjual = $penjual['idPenjual'];
			$query = "UPDATE  `Pengguna` SET  `poin_rating-toko` = `poin_rating-toko`+10  WHERE  `idPengguna` = $idpenjual";
			echo $query."<br />";
			$db->exec($query);
		}
	}
}
